<template>
    <div class="main">
        <!--<div class="header_title">验证注册手机</div>-->
        <div class="register-form">
            <div class="phone">
                <van-field v-model="data.ChnName" label="姓名" placeholder="请输入姓名" required />
                <van-field v-model="data.IDCard" label="身份证" required placeholder="请输入身份证" />
                <van-field v-model="data.Mobile" label="手机号" readonly />
                <van-field v-model="data.CompanyName" label="施工单位" placeholder="请选择施工单位" required @click-input="goConstructItem" readonly />
                <van-field class="input_text" name="uploader" label="照片">
                    <template #input>
                        <eagle-image-uploader :images.sync="data.Head" :cnt="1"></eagle-image-uploader>
                    </template>
                </van-field>
            </div>

            <div style="margin: 20px">
                <van-button class="br-5" block type="info" @click="submit()">
                    提交
                </van-button>
            </div>
        </div>
        <van-popup v-model="EnterpriseShow" closeable round position="bottom" :style="{ height: '80%' }">
            <div class="popup-title">选择施工单位</div>
            <div class="popup-content">
                <ConstructsafeCompanyChoose @CallBack="_chooseEnterpriseCb" :CompanyCode="CompanyCode" />
            </div>
        </van-popup>
    </div>
</template>

<script>
import { addSGFCompanyUser } from "@/api/public";
import ConstructsafeCompanyChoose from "@/views/constructsafeCompany/components/WindowChoose.vue";
export default {
    components: {
        ConstructsafeCompanyChoose,
    },
    data() {
        return {
            data: {
                ChnName: "",
                IDCard: "",
                Mobile: "",
                CompanyCode: "",
                CompanyName: "",
                Head: "",
            },
            CompanyCode: "",
            EnterpriseShow: false,
        };
    },
    created() {
        this.CompanyCode = this.$route.query.CompanyCode;
        this.data.Mobile = this.$route.query.mobile;
    },
    methods: {
        goConstructItem() {
            this.EnterpriseShow = true;
        },
        _chooseEnterpriseCb(item) {
            this.$set(this.data, "CompanyName", item.name || "");
            this.$set(this.data, "CompanyCode", item.value || "");
            this.EnterpriseShow = false;
        },
        submit() {
            if (!this.data.ChnName) {
                return this.$toast("请输入姓名");
            }
            if (!this.data.IDCard) {
                return this.$toast("请输入身份证");
            }
            if (!this.data.Mobile) {
                return this.$toast("请输入手机号");
            }
            if (!this.data.CompanyName) {
                return this.$toast("请选择施工单位");
            }
            addSGFCompanyUser(this.data)
                .then((res) => {
                    this.$toast.success("注册成功");
                    this.$toast("验证成功,即将跳转");
                    this.$store.commit("setBaseUrl", process.env.VUE_APP_BASE_API);
                    // 修改登陆状态
                    this.$store.commit("updateLogin", true);
                    // 把token存入store
                    this.$store.commit("updateToken", res.data.UserName);
                    // 把用户名存入store
                    this.$store.commit("updateUsername", res.data.ChnName);
                    // 保存地址 updateArea
                    this.$store.commit("updateArea", res.data.ApplyArea);
                    // 保存地址 updateArea
                    this.$store.commit("updateCompanySysType", res.data.CompanySysType);
                    // 公司code
                    this.$store.commit("updateCompanyCode", res.data.CompanyCode);
                    this.$store
                        .dispatch("getRoles", res.data.UserKind)
                        .then((data) => {
                            console.log(data);
                        })
                        .catch((_) => {
                            failure("获取失败");
                        });
                    this.$router.push("/index?num=" + Math.ceil(Math.random() * 100000));
                })
                .catch((err) => {
                    console.log("err", err);
                });
        },
    },
};
</script>

<style lang="less" scoped>
/*.register-form {*/
/*    margin-top: 10px;*/
/*}*/
.phone {
    position: relative;

    .code {
        position: absolute;
        right: 20px;
        top: 20px;
        font-size: 12px;
    }
}
</style>



